package com.test.resumeanalysis.view

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.RelativeLayout
import android.widget.TextView

import com.test.resumeanalysis.R


class ResumePopupWindow(
    private val mActivity: Activity,
    private var mOnClickListener: OnClickListener
) :
    PopupWindow() {
    fun setPopupOnClickListener(mPopupOnClickListener: OnClickListener) {
        mOnClickListener = mPopupOnClickListener
    }

    interface OnClickListener {
        fun picOnClick()
        fun pdfOnClick()
        fun cancel() //取消
    }

    private var PaymentCodeTitle //弹窗的标题
            : String? = null


    private var dialogView: View? = null
    private var dialogMessage: String? = null

    init {
        val view = LayoutInflater.from(mActivity).inflate(R.layout.popup_window_layout, null)
        contentView = view
        width = RelativeLayout.LayoutParams.MATCH_PARENT
        height = RelativeLayout.LayoutParams.WRAP_CONTENT

        setBackgroundDrawable(BitmapDrawable())

        isFocusable = true
        isOutsideTouchable = true

        animationStyle = R.style.popup_theme


        setOnDismissListener { dismissPopupWindow() }

        setOnPopupViewClick(view)

        setBackgroundAlpha(0.5f)
    }

    fun show(parentView: View?) {
        //设置位置
        showAtLocation(parentView, Gravity.BOTTOM, 0, 66)
    }

    fun setBackgroundAlpha(alpha: Float) {
        val lp = mActivity.window.attributes
        lp.alpha = alpha
        mActivity.window.attributes = lp
    }

    fun setDialogMessage(mag: String?) {
        dialogMessage = mag
    }

    private fun dismissPopupWindow() {
        dismiss()
        setBackgroundAlpha(1f)
    }

    /**
     * 弹窗点击事件
     *
     * @param view
     */
    fun setOnPopupViewClick(view: View) {
        dialogView =
            LayoutInflater.from(mActivity).inflate(R.layout.dialog_payment_qr_code_layout, null)
        val iv = dialogView?.findViewById<ImageView>(R.id.iv_payment_code)

        /**
         * 图片解析
         */
        val tv_Alipay = view.findViewById<View>(R.id.tv_Alipay) as TextView
        tv_Alipay.setOnClickListener { v: View? ->
            dismissPopupWindow()
            PaymentCodeTitle = "图片解析"
            mOnClickListener.picOnClick()
        }
        /**
         * docx解析
         */
        val tv_WeChat = view.findViewById<View>(R.id.tv_WeChat) as TextView
        tv_WeChat.setOnClickListener { v: View? ->
            dismissPopupWindow()
            PaymentCodeTitle = "docx解析"
            //            showPaymentCode(PAY_TYPE_WeChat);\
            mOnClickListener.pdfOnClick()
        }
        val tv_cancel = view.findViewById<View>(R.id.tv_cancel) as TextView
        tv_cancel.setOnClickListener { v: View? ->
            //取消
            dismissPopupWindow()
            mOnClickListener.cancel()
        }
    }
}